var getPermutation = function(n, k) {
    if(n == 1){
        return '1';
    }
    var nums = [];
    for(var i = 0;i <= n;i++){
        nums[i] = 0;
    }
    var facorial = [];
    facorial[0] = 0;
    facorial[1] = 1;
    for(var i = 2;i <= n;i++){
        facorial[i] = facorial[i-1]*i;
    }
    var str = [];
    if(k >= facorial[n]){

        for(var i = n;i>0;i--){
            str.push(i);
        }
        return str.join('');
    }
    var times = 0;
    while(times <= n){
        times++;
        var topNum = Math.floor(k/facorial[n-times]);
        var mod = k % facorial[n-times];
        if(mod != 0){
            str.push(topNum+1);
            nums[topNum+1] = times;
        }else{
            str.push(topNum);
            nums[topNum] = times;
            times++;
            for(var i = n;i > 0;i--){
                if(nums[i] == 0){
                    nums[i] = times;
                    times++;
                }
            }
        }
        k = mod;
    }
    for(var i = 1;i <= n;i++){
        str[nums[i]] = i;
    }
    str.shift();
    return str.join('');
};
console.log(getPermutation(3,1));